package com.car.loan.project.xr.service.impl;

import com.car.loan.common.exception.loan.LoanParameterException;
import com.car.loan.common.utils.SecurityUtils;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.system.service.ISysSequenceService;
import com.car.loan.project.xr.domain.LoanDetail;
import com.car.loan.project.xr.domain.LoanStatus;
import com.car.loan.project.xr.mapper.LoanDetailMapper;
import com.car.loan.project.xr.mapper.LoanStatusMapper;
import com.car.loan.project.xr.util.enums.ContractAuditStatus;
import com.car.loan.project.xr.util.enums.FileAuditStatus;
import io.swagger.annotations.ApiModel;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author huangding
 * @description
 * @date 2019/12/22 22:20
 */
@Service
public class FileAuditService {


    @Autowired
    private LoanStatusMapper loanStatusMapper;

    @Autowired
    private LoanDetailMapper loanDetailMapper;

    @Autowired
    private ISysSequenceService sequenceArchivesServiceImpl;

    @Transactional(rollbackFor = Exception.class)
    public synchronized String getFileNo(Long loanId) {
        Optional<LoanDetail> optionalLoanDetail = loanDetailMapper.selectByLoanId(loanId);
        LoanDetail loanDetail = optionalLoanDetail.get();
        if (StringUtils.isNotBlank(loanDetail.getFileNo())) {
            return loanDetail.getFileNo();
        }
        loanDetail.setContractFilingDate(LocalDate.now().toString());
        loanDetail.setFileNo(sequenceArchivesServiceImpl.next());
        loanDetailMapper.updateById(loanDetail);
        return loanDetail.getFileNo();
    }

    @Transactional(rollbackFor = Exception.class)
    public synchronized AjaxResult audit(Long loanId) {
        Optional<LoanStatus> optionalLoanStatus = loanStatusMapper.selectByLoanId(loanId);
        LoanStatus loanStatus = optionalLoanStatus
            .orElseThrow(() -> new LoanParameterException("该业务状态数据异常，请联系管理员"));

        Long fileAuditStatus = loanStatus.getFileAuditStatus();
        if (FileAuditStatus.INIT.status != fileAuditStatus) {
            return AjaxResult.error("当前状态不可提交");
        }

        loanStatus.setFileAuditStatus((long) FileAuditStatus.SUBMIT.status);
        loanStatus.setFileAuditTime(LocalDateTime.now());
        loanStatus.setFileAuditId(SecurityUtils.getLoginUserId());
        loanStatusMapper.updateById(loanStatus);
        Optional<LoanDetail> optionalLoanDetail = loanDetailMapper.selectByLoanId(loanId);
        LoanDetail loanDetail = optionalLoanDetail.get();
        if (StringUtils.isNotBlank(loanDetail.getFileNo())) {
            return AjaxResult.success(loanDetail.getFileNo());
        }
        loanDetail.setContractFilingDate(LocalDate.now().toString());
        loanDetail.setFileNo(sequenceArchivesServiceImpl.next());
        loanDetailMapper.updateById(loanDetail);
        return AjaxResult.success(loanDetail.getFileNo());
    }
}
